<?Lassoscript
// Last modified 4/17/09 by Eric Landmann

// CHANGE NOTES
// 4/17/09
// Version 1.0

Include:'siteconfig.lasso';
Include:($svLibsPath)'sessions.inc'; 

// Params passed to this from thread.lasso are:
// TID			Topic
// SID			Section
// mID			Member ID
// SendEmail	Send Email (Y or N)

// Debugging
// Var:'svDebug' = 'Y';

// Transferring Action_Params to variables 
Var:'svSID'=(Action_Param:'SID');
Var:'svTID'=(Action_Param:'TID');
// Get $svMemberID from the session
Var:'vSendEmail'=(Action_Param:'SendEmail');

// Setting default value of SendEmailNew so if there is no record it is set to Y
Var:'SendEmailNew' = 'Y';

// Redirect to home page if the referrer is not thread.lasso or member ID is blank
If: ((Referrer_URL) !>> 'thread.lasso') || ((Var:'svMemberID') == '');
	Var:'URL' =  (($svSiteFolderRoot)'/forums.lasso');
		If: $svDebug == 'Y';
			Debug;
				'26: Redirect: <a href="'$URL'">' ($URL) '</a><br>';
				Include:($svLibsPath)'vardumpalpha.lasso';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
Else;

		// Updating the Notify Table
		
		// See if a there is a record for this Member and this thread
		Var:'SQLQueryNotify'='SELECT * FROM ' $svNotifyTable ' WHERE Topic_ID = "' ($svTID) '" AND ID_Member = "' ($svMemberID) '" LIMIT 1';

		Inline: $IV_SearchNotify, -Table=$svNotifyTable, -SQL=$SQLQueryNotify;

			Debug;
				'41: SQLQueryNotify = ' ($SQLQueryNotify) '<br>\n';
			/Debug;
		
			// If Found_Count > 0, update the record, else add it
			If: (Found_Count) > 0;

				// Copy the ID field for update action
				Var:'ThisID' = (Field:'ID');

				// Toggle the Email Value
				Var:'SendEmailCurrrent' = (Field:'SendEmail');
				If: (Var:'SendEmailCurrrent') == 'N';
					Var:'SendEmailNew' = 'Y';
				Else;
					Var:'SendEmailNew' = 'N';
				/If;				

				// Create the Notify Update query
				Var:'SQLUpdateNotify' = 'UPDATE ' $svNotifyTable ' SET SendEmail = "'($SendEmailNew)'", DateModified = "'(Server_Date:-Extended)'" WHERE Topic_ID="' $svTID '" AND ID_Member ="' (Var:'svMemberID') '"';
				
				Inline: $IV_SearchNotify, -Table=$svNotifyTable, -SQL=$SQLUpdateNotify;

					// If update successful, generate error 4002 "E-mail Preferences Saved"
					If: (Error_CurrentError) == (Error_NoError);
						Var:'vError' = '4002';
					Else;
						Var:'vError' = '5023';
						Var:'vOption' = (Error_CurrentError);
					/If;
					Debug;
						'69: vError = ' ($vError) '<br>\n';
						'69: SQLUpdateNotify = ' ($SQLUpdateNotify) '<br>\n';
					/Debug;

				/Inline;

			// Otherwise add the record to the Notify table
			Else;
				// Create the Notify Add query
				Var:'SQLAddNotify'='INSERT INTO '($svSiteDatabase) '.' ($svNotifyTable)' SET ID_Member = "'($svMemberID)'",
				Topic_ID = "'($svTID)'",
				SendEmail = "'($SendEmailNew)'",
				DateModified = "'(Server_Date:-Extended)'"';
				Inline: $IV_SearchNotify, -Table=$svNotifyTable, -SQL=$SQLAddNotify;

					// If update successful, generate error 4002 "E-mail Preferences Saved"
					If: (Error_CurrentError) == (Error_NoError);
						Var:'vError' = '4002';
					Else;
						Var:'vError' = '5023';
						Var:'vOption' = (Error_CurrentError);
					/If;
					Debug;
						'69: vError = ' ($vError) '<br>\n';
						'69: SQLUpdateNotify = ' ($SQLUpdateNotify) '<br>\n';
					/Debug;

				/Inline;

			/If;

		/Inline;

		// Redirect back to the Thread page
		Var:'URL' = ('thread.lasso?Error='$vError'&'(Client_POSTArgs));
		If: $svDebug == 'Y';
			Debug;
				'91: URL = ' $URL '<br>\n';
				'91: Redirect: <a href="'$URL'">' ($URL) '</a><br>';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;
?>